﻿namespace PowerSong.BibleSupport.FileArchive
    {
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// Represents a table of files contained in an archive.
    /// </summary>
    public class FileTable
        {

        private List<EntryData> files = new List<EntryData>();

        internal EntryData GetFile(int index)
            {
            return files[index];
            }

        internal string[] VirtualFileNames
            {
            get
                {
                string[] result = new string[files.Count];
                for (int i = 0; i <= files.Count - 1; i++)
                    {
                    result[i] = files[i].VirtualFileName;
                    }
                return result;
                }
            }

        internal int FileCount
            {
            get { return files.Count; }
            }

        public void AddFile(string virtualFileName, byte[] content)
            {
            files.Add(new EntryData(virtualFileName, content, 0));
            }

        internal void AddFile(string virtualFileName, byte[] content, int position)
            {
            files.Add(new EntryData(virtualFileName, content, position));
            }

        internal void SetFilePosition(string virtualFileName, int position)
            {
            foreach (var file in files)
                {
                if (file.VirtualFileName == virtualFileName) file.Position = position;
                }
            }

        internal int GetFilePosition(string virtualFileName)
            {
            foreach (EntryData file in files)
                {
                if (file.VirtualFileName == virtualFileName) return file.Position;
                }
            throw new ApplicationException("Cannot find the file: '" + virtualFileName + "' in the archive.");
            }

        }

    }
